﻿
module Sustainalytics.Portfolio {

    import Common = Sustainalytics.Common;

    export class PortfolioSearchService {

        private $http: ng.IHttpService;
        private $rootScope: ng.IScope;

        private accountId: string;
        private userId: string;

        constructor($http: ng.IHttpService, $rootScope: ng.IScope) {
            this.$http = $http;
            this.$rootScope = $rootScope;

            if (this.$rootScope['userInfo'] == null) {
                this.$rootScope['userInfo'] = angular.fromJson(localStorage.getItem('sust_userInfo'));
            }
            this.accountId = this.$rootScope['userInfo'].accountId;
            this.userId = this.$rootScope['userInfo'].userId;
        }

        public SearchPortfolio(term: string, skip: number, take: number): ng.IHttpPromise<PortfolioDto[]> {

            var options = new Common.ListOptions();
            options.skip = skip;
            options.take = take;
            options.orderBy = ["Name"];
            options.orderAscending = true;
            options.includeAllFields = false;
            options.getCount = false;

            var params = $.param({
                isMyPortfolio: true,
                namePortfolio: term,
                sharingStatus: null,
                isNameEqual: false,
                accountId: this.accountId,
                userId: this.userId,
                listOptions: options
            });

            var url: string = '/portfolioserv/api/v1/portfolios?' + params;

            var lstPromise: ng.IHttpPromise<PortfolioDto[]> = this.$http.get(url);

            return lstPromise;
        }

        public SearchScreeningToolPortfolio(term: string, skip: number, take: number): ng.IHttpPromise<PortfolioDto[]> {

            var options = new Common.ListOptions();
            options.skip = skip;
            options.take = take;
            options.orderBy = ["Name"];
            options.orderAscending = true;
            options.includeAllFields = false;
            options.getCount = false;

            var params = $.param({
                isMyPortfolio: null,
                namePortfolio: term,
                sharingStatus: null,
                isNameEqual: false,
                accountId: this.accountId,
                userId: this.userId,
                listOptions: options,
                screeningTool: true
            });

            var url: string = '/portfolioserv/api/v1/portfolios?' + params;

            var lstPromise: ng.IHttpPromise<PortfolioDto[]> = this.$http.get(url);

            return lstPromise;
        }
    }

    var portfolioSearchModule = angular.module('Sustainalytics.Portfolio');

    portfolioSearchModule.factory('PortfolioSearchService', ($http: ng.IHttpService, $rootScope: ng.IScope) => {
        var portfolioSearchService: Sustainalytics.Portfolio.PortfolioSearchService = new Sustainalytics.Portfolio.PortfolioSearchService($http, $rootScope);
        return portfolioSearchService;
    });
}